import { VoucherModel } from "@/api/voucher";

export function showLightPrompt(msg, type = "warning") {
  ElMessage({
    message: msg,
    type: type,
  });
}

export async function getVoucherUrlInfo(signType = 0) {
  try {
    // const cachedData = sessionStorage.getItem("PayVoucherUrl");
    // if (cachedData) {
    //   const parsedData = JSON.parse(cachedData);
    //   return {
    //     voucherurl: parsedData.voucherurl,
    //     stampurl: parsedData.stampurl,
    //   };
    // }
    let res = await VoucherModel.getVoucherUrl();
    res.data = [];
    if (res.data && res.data.length > 0) {
      const obj = res.data[0];
      const voucherInfo = {
        voucherurl: obj.voucherurl,
        stampurl: obj.stampurl,
      };
      if (!voucherInfo.voucherurl) {
        showLightPrompt("获取电子凭证库服务器地址失败", "error");
        return;
      }
      if (!voucherInfo.voucherInfo) {
        let msgTxt = signType == 0 ? "获取电子签名服务器地址失败" : "获取电子签章服务器地址失败";
        showLightPrompt(msgTxt, "error");
        return;
      }

      sessionStorage.setItem("PayVoucherUrl", JSON.stringify(voucherInfo));
      return voucherInfo;
    } else {
      showLightPrompt("获取电子凭证库服务器及电子签名服务器地址失败", "error");
      return;
    }
  } catch (error) {
    console.error('获取凭证 URL 信息失败:', error.reason);
    //throw error;
    return
  }
}
